package com.ftg.youx.config;

import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.flow.FlowException;
import com.alibaba.fastjson.JSON;
import com.ftg.youx.tools.R;
import com.ftg.youx.tools.S;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;

import java.io.PrintWriter;

/**
 * 哨兵异常处理
 *
 * @author 布衣
 */
@Component
public class SentinelExceptionHandler implements BlockExceptionHandler {

    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, BlockException e) throws Exception {

        if(e instanceof FlowException){
            throw new FlowException("abc");
        }
        //        response.setStatus(200);
//        response.setCharacterEncoding("utf-8");
//        response.setContentType("application/json;charset=utf-8");
//        PrintWriter out = response.getWriter();
//        String msg = JSON.toJSONString(R.build(S.SENTINEL_BLOCK, S.SENTINEL_BLOCK.getMsg()));
//        out.print(msg);
//        out.flush();
//        out.close();
    }
}
